package com.jzlin.funhub.admin.service.system;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzlin.funhub.admin.mapper.SysMessageMapper;
import com.jzlin.funhub.admin.model.dto.SysMessageSessionNotReadDTO;
import com.jzlin.funhub.admin.model.entity.SysMessage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 站内私信表 服务实现类
 * </p>
 *
 * @author jzlin
 * @since 2025-08-26
 */
@Service
public class SysMessageService extends ServiceImpl<SysMessageMapper, SysMessage> {
    public List<SysMessage> listBySessionId(Long sessionId) {
        return this.query().eq("session_id", sessionId).orderByAsc("create_time").list();
    }

    /**
     * 查询用户所有会话的未读消息数
     * @param userId
     * @return
     */
    public List<SysMessageSessionNotReadDTO> countSessionNotRead(Long userId) {
        return baseMapper.countSessionNotReadByUserId(userId);
    }

    /**
     * 用户指定会话的消息设置为已读
     * @param sessionId 会话id
     * @param userId 用户id
     * @return
     */
    public void readMessageBySessionAndUser(Long sessionId, Long userId) {
        this.update().eq("session_id", sessionId).eq("to_user_id", userId).set("is_read", 1).update();
    }
}
